(function (w) {
    var g = {};
    g.data = {
        detail: {
            comment: {
                curPage: 1,
                size: 6,
                itemId: 0,
                count:0,
                data: {}
            }
        }
    };
    g.init = function () {
        switch (config.action) {
            case 'lists':
                g.load.lists();
                break;
            case 'detail':
                // 鍟嗗搧璇勮鍔犺浇
                mcake.api.get('88e654530e1851cf', {
                    twoId: $('.detail-content').eq(0).data('id'),
                    page: g.data.detail.comment.curPage,
                    size: g.data.detail.comment.size
                }, '', function (ret) {
                    switch (ret.code) {
                        case 1:
                            g.data.detail.comment.data = ret.data;
                            g.data.detail.comment.count = ret.data.count;
                            break;
                        default:
                    }
                });
                g.load.detail.init();
                break;
            default:

        }

    };
    g.load = {
        lists: function () {
            goods.event.lists();
        },
        detail: {
            init: function () {
                goods.event.detail.init();
                this.comment();
            },
            comment: function () {
                $('.goods-comment').html('');
                $('#goods-comment-tmpl').tmpl(g.data.detail.comment).appendTo('.goods-comment');
                var pageCount = Math.ceil(g.data.detail.comment.count / g.data.detail.comment.size);
                if (pageCount > 1){
                    $('#page').pagination({
                        pageCount: pageCount,
                        jump: false,
                        coping: true,
                        current: g.data.detail.comment.curPage,
                        count: 5,
                        prevContent: '涓婁竴椤�',
                        nextContent: '涓嬩竴椤�',
                        callback: function (api) {
                            var curPage = api.getCurrent();
                            if (g.data.detail.comment.curPage === curPage) {
                                return false;
                            }
                            g.data.detail.comment.curPage = curPage;
                            mcake.api.get('88e654530e1851cf', {
                                twoId: $('.detail-content').eq(0).data('id'),
                                page: g.data.detail.comment.curPage,
                                size: g.data.detail.comment.size,
                                itemId: g.data.detail.comment.itemId
                            }, null, function (ret) {
                                switch (ret.code) {
                                    case 1:
                                        g.data.detail.comment.data = ret.data;
                                        break;
                                    default:

                                }
                            });
                            g.load.detail.comment();
                            $(document).scrollTop($('.item-pingjia .goods-comment')[0].offsetTop + 600);
                        }
                    });
                }
                $(".img-class").click(function(){
                    $(this).addClass('show-big').siblings().removeClass('show-big');
                    $(this).parent('.p3').next().fadeIn(0);
                    $(this).parent('.p3').next().find('.big-img').fadeIn(0);
                    $(this).parent('.p3').next().find('.big-img').attr('src', $(this).attr('url') + "?x-oss-process=image/auto-orient,1")
                });
                $(".big-img").click(function(){
                    $(this).fadeOut(0);
                });
                g.event.detail.comment();
            }
        }
    };
    g.event = {
        lists: function () {
            //  鍔犲叆璐墿杞�
            $('.add-car')
                .click(function () {
                   /* if (!mcake.user.isLogin) {
                        mcake.msg.error('璇风櫥褰曞悗鍐嶆搷浣�');
                        $('body,html').animate({scrollTop: 0}, 500);
                        $('.login').click();
                        return false;
                    }*/
                    var data = JSON.parse(util.Base64.decode($(this)
                        .data('data')));

                    $.each(data.list, function (i, v) {
                        data.list[i].text = v.spec + (v.edible ? '-' + v.edible : '');
                        data.list[i].data = util.Base64.encode(JSON.stringify(v));
                    });
                    $('#pop').html('');
                    $('#pop-goods-select').tmpl(data).appendTo('#pop');
                    $('.bg').show();
                    $('#pop .cart-box').show(300);

                    // 纭畾
                    $('.sure')
                        .click(function () {
                            var data = JSON.parse(util.Base64.decode($('#pop').find('.cart-box .box-02 select').find('option:selected').data('data')));
                            var id = data.id;
                            if (isNaN(id)){
                                mcake.msg.error('瀵逛笉璧凤紝璇ュ晢鍝佸凡涓嬫灦鎴栧敭缃�');
                                return false;
                            }
                            var num = $('#pop')
                                .find('.cart-box .box-03 .number')
                                .text() - 0;
                            var cityId = util.cookie.get('cityId');
                            mcake.api.post('0a726336d3a19773', {
                                id: id,
                                num: num,
                                cityId: cityId,
                                method: 0,
                                uid: mcake.user._uid
                            }, null, function (ret) {
                                switch (ret.code) {
                                    case 1:
                                        mcake.msg.success('娣诲姞鍒拌喘鐗╄溅鎴愬姛');
                                        cart.data = ret.data;
                                        cart.load.topCart();
                                        break;
                                    default:
                                        mcake.msg.error(ret.msg);
                                        break;
                                }
                            });
                            $('.bg').hide();
                            $('.cart-box').hide();
                        });

                    // 鍙栨秷
                    $('.cancel,.close-x')
                        .click(function () {
                            $('.bg').hide();
                            $('.cart-box').hide();
                        });

                    // 鍔�
                    $('.jia')
                        .click(function () {
                            var n = $('.number').html();
                            var num = parseInt(n);
                            if (num > 49) {
                                return;
                            }
                            num++;

                            $('.number').html(num);
                        });

                    // 鍑�
                    $('.jian')
                        .click(function () {
                            var n = $('.number').html();
                            var num = parseInt(n);
                            if (num < 2) {
                                return;
                            }
                            num--;

                            $('.number').html(num);
                        });

                    // 閫夋嫨
                    $('.box-02 select')
                        .change(function () {
                            var data = JSON.parse(util.Base64.decode($(this).find('option:selected').data('data')));
                            $('.cart-box .cart-price').text(data.pprice);
                            // 鍒囨崲閰嶄欢
                            $('.cart-box .cart-fittings').text(data.fittings);
                        });
                });
        },
        detail: {
            init: function () {
                this.comment();
                $('.pick-pond').click(function () {
                    var data = JSON.parse(util.Base64.decode($(this).parent().data('data')));
                    var id = parseInt($(this).data('id'));
                    if ($(this).hasClass('pond-cur')) {
                        return false;
                    }
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].id == id) {
                            if($('#p-btn-sq').data('val') == 1) {
                                if(data[i].useFlg != 1) {
                                    $('#p-btn-sq').show();
                                    $('#p-btn').hide();
                                } else {
                                    $('#p-btn-sq').hide();
                                    $('#p-btn').show();
                                }
                            }
                            $('.span01').text(data[i].ahead);
                            $('.size').text('SIZE锛�' + data[i].size);
                            if (data[i].fittings[51]){
                                $('.tableware').html('鍚�'+data[i].fittings[51]['num']+data[i].fittings[51]['uname']+data[i].fittings[51]['name']);
                            }
                            var fittings = '';
                            $.each(data[i].fittings, function () {
                                if (this.price == '0.00') {
                                    fittings += this.num + this.uname + this.name + ' ';
                                }
                            });
                            // $('.info .info-left .fittings').text(fittings);
                            $('.detail-box .top-right .span-rmb').text(data[i].salePrice);
                            if (data[i].pprice != data[i].salePrice) {
                                $('.detail-box .top-right .span-sale').text('锟�'+data[i].pprice);
                            } else {
                                $('.detail-box .top-right .span-sale').text('');
                            }
                            break;
                        }
                    }
                    $(this).addClass('pond-cur').siblings().removeClass('pond-cur');
                });
                $('.pingjia-span').click(function () {
                    $(document).scrollTop($('.item-pingjia .goods-comment')[0].offsetTop + 300);
                });
                $('.add-cart').click(function () {
                    var id = parseInt($('.pick-pond.pond-cur').data('id'));
                    if (isNaN(id)){
                        if ($(this).attr('data-sku') == 'B1004') {
                            layer.alert('鏈堥ゼ鍗¤鎷ㄦ墦 <a href="tel:4006-678-678" style="color:#2700ff;">4006-678-678</a> 鍏戞崲');
                            return false;
                        }
                        mcake.msg.error('瀵逛笉璧凤紝璇ュ晢鍝佸凡涓嬫灦鎴栧敭缃�');
                        return false;
                    }
                    var data = JSON.parse(util.Base64.decode($('.pond').data('data')));

                    var product_name = '';
                    var product_price = 0;
                    $.each(data, function (i, v) {
                        if (v.id == id) {
                            product_name = v.name;
                            product_price = v.salePrice;
                        }
                    });
                    _hmt.push(['_trackCustomEvent', 'add_to_cart', {
                            "product_name": product_name,
                            "product_price": product_price
                        }
                    ]);

                    mcake.api.post('0a726336d3a19773', {
                        id: id,
                        num: 1,
                        cityId: util.cookie.get('cityId'),
                        method: 0,
                        uid: mcake.user._uid
                    }, null, function (ret) {
                        switch (ret.code) {
                            case 1:
                                mcake.msg.success('娣诲姞鍒拌喘鐗╄溅鎴愬姛');
                                cart.data = ret.data;
                                cart.load.init();
                                break;
                            default:
                                mcake.msg.error(ret.msg);
                                break;
                        }
                    });
                });

                $('.buy-now').click(function () {
                    var id = parseInt($('.pick-pond.pond-cur').data('id'));
                    if (isNaN(id)){
                        if ($(this).attr('data-sku') == 'B1004') {
                            layer.alert('鏈堥ゼ鍗¤鎷ㄦ墦 <a href="tel:4006-678-678" style="color:#2700ff;">4006-678-678</a> 鍏戞崲');
                            return false;
                        }
                        mcake.msg.error('瀵逛笉璧凤紝璇ュ晢鍝佸凡涓嬫灦鎴栧敭缃�');
                        return false;
                    }
                    var data = JSON.parse(util.Base64.decode($('.pond').data('data')));

                    var product_name = '';
                    var product_price = 0;
                    $.each(data, function (i, v) {
                       if (v.id == id) {
                           product_name = v.name;
                           product_price = v.salePrice;
                       }
                    });
                    _hmt.push(['_trackCustomEvent', 'buy_now', {
                            "product_name": product_name,
                            "product_price": product_price
                        }
                    ]);

                    mcake.api.post('0a726336d3a19773', {
                        id: id,
                        num: 1,
                        cityId: util.cookie.get('cityId'),
                        method: 0,
                        uid: mcake.user._uid
                    }, null, function (ret) {
                        switch (ret.code) {
                            case 1:
                                mcake.msg.success('娣诲姞鍒拌喘鐗╄溅鎴愬姛');
                                window.location.href = '/cart';
                                break;
                            default:
                                mcake.msg.error(ret.msg);
                                break;
                        }
                    });
                });
            },
            comment: function () {
                $('.pingjia-top span').click(function () {
                    if ($(this).data('count') === 0) {
                        return false;
                    }
                    g.data.detail.comment.itemId = $(this).data('id');
                    g.data.detail.comment.count = $(this).data('count');
                    g.data.detail.comment.curPage = 1;
                    var data = {
                        twoId: $('.detail-content').eq(0).data('id'),
                        page: g.data.detail.comment.curPage,
                        size: g.data.detail.comment.size
                    };
                    if (g.data.detail.comment.itemId !== 0) {
                        data.itemId = g.data.detail.comment.itemId;
                    }
                    mcake.api.get('88e654530e1851cf', data, null, function (ret) {
                        switch (ret.code) {
                            case 1:
                                g.data.detail.comment.data = ret.data;
                                break;
                            default:
                        }
                    });
                    g.load.detail.comment();
                });
            }
        }
    };

    if (typeof module !== 'undefined' && module.exports) {
        module.exports = g;
    } else if (typeof exports !== 'undefined') {
        exports.goods = g;
    } else if (typeof define === 'function' && define.amd) {
        define([], function () {
            return g;
        });
    } else if (typeof w.goods === 'undefined') {
        w.goods = g;
    }
})(window);